package control;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import server.mysqlConnection;

/**
 * @author Limor
  *This class responsible for the operation create new reader card, when the client wants to activate this 
  *operation he insert data for the new subscriber in the GUI client. The data inserted into string array that will send to the server.
  * In the server we maps the message by checking the first string (see AbstractControl).
 */
public class ReaderCardControl extends AbstractControl
{
	private String SubscriberExist; 

	public Object anlyze(String[] msg) 
	{
		ArrayList<Object> rslt=new ArrayList<Object>();
		rslt.add("newReaderCard");
		SubscriberExist=msg[1];
		if(checkExist())
		{
			Calendar graduateDate = Calendar.getInstance();
			graduateDate.add(Calendar.YEAR, 4);
			DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
			String graduateD = "'"+dateFormat.format(graduateDate.getTime())+"'" ;			
			String UpdateSubscriberDB=new String("INSERT into test.subscriber SET "+"sId="+"'"+msg[1]+"'"+",CardStatus=1"+", GraduateDate="+graduateD+";");
			mysqlConnection.update(UpdateSubscriberDB);
			String query=new String("INSERT into test.users SET "+"ID="+"'"+msg[1]+"'"+ ",FirstName="+"'"+msg[2]+"'"+ ",LastName="+"'"+msg[3]+"'"+",Phone="+"'"+msg[4]+"'"+", email="+"'"+msg[5]+"'"+",access_level=1, password='12345678';");
			mysqlConnection.update(query);
			rslt.add("Add Successfully");
			return rslt;
		}
		rslt.add(new String("Subscriber already exist"));
		return rslt;	
	}

	
	@Override
	public Boolean checkExist() 
	{
		String query=new String("SELECT ID FROM test.users WHERE "+"ID="+SubscriberExist+";");
		String ans=mysqlConnection.check(query);
		if(ans.contentEquals("NotEXIST")) return true;
		else return false;
	}

}
